#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int nameToIndex(char* name){
    return (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3]-'0');
}
vector<int> courses[175760];
int main(){
    int N,K;
    scanf("%d %d",&N,&K);
    int index=0;
    while(K-->0){
        int courseId,nstudents;
        scanf("%d %d",&courseId,&nstudents);
        while(nstudents-->0){
            char name[5];
            scanf("%s",name);
            courses[nameToIndex(name)].push_back(courseId);
        }
    }
    while(N-->0){
        char name[5];
        scanf("%s",name);
        vector<int> res = courses[nameToIndex(name)];
        sort(res.begin(),res.end());
        printf("%s %d",name,res.size());
        for(int i=0;i<res.size();i++){
            printf(" %d",res[i]);
        }
        printf("\n");
    }
    return 0;
}
